<?php

namespace App\Http\Controllers\Home;

use App\Models\HotWords;
use App\Models\Navigation;
use App\Models\OpenScreen;
use App\Models\OperatePosition;
use App\Models\Rage;
use App\Models\Recommend;
use App\Models\WheelPicture;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{

    public function index()
    {
        /*$userInfo =getMemberIdByToken('c063b7ae6266a660fcbba02eb98e531d');
       // var_dump($userInfo->id);
        $res=DB::table("search")->where(['member_id'=>$userInfo->id])->first(['id','key_words']);
        //$res->key_words='项链,手表,袜子';
        $a=DB::table("search")
        ->where('id', $res->id)
        ->update(['key_words'=>'项链,手表,袜子']);*/
//array_unshift($a,6);
       // $sh=explode(',',$res->key_words);
        //$sh=array_slice($sh,0,2);
//dd($sh);
       /* $hotWords =HotWords::query()
            ->select(['show_hot_word','search_word','font_color'])
            ->where('start_time','<=',date('Y-m-d H:i:s'))
            ->where('end_time','>',date('Y-m-d H:i:s'))
            ->limit(12)
            ->orderBy('weight','desc')
            ->get()
            ->toArray();
        dd($hotWords);*/
      /* //随机获取数据
       $Recommend=Recommend::query()
            ->select('recommend_word','search_word')
            ->inRandomOrder()
           ->first()->toArray();
        dd($Recommend);*/
        /*$Recommend=Recommend::query()
            ->select('recommend_word','search_word')
            ->where('start_time','<=',date('Y-m-d H:i:s'))
            ->where('end_time','>',date('Y-m-d H:i:s'))
            ->orderBy('weight','desc')
            ->first()->toArray();
        dd($Recommend);*/
        die();
        dd(\App\Enums\ShelfStatusEnum::$listExplain);
        $param = request()->all();
        $validator = \Validator::make($param, [     //正则验证
            'id'   => 'required |integer',
            'sort' => 'in:asc,desc',//required |
            'orderBy' => 'required | integer',
            'page' => 'integer',
            'pageSize' => 'integer'
        ]);
        $error = $validator->errors();
        dd($error);
        $res=DB::table('goods')
            //->where('id','=',11)
            ->orderBy('tao_sale','desc')
            ->orderBy('coupon_amount','desc')
            ->orderBy('actual_price','desc')
            ->paginate(2)
            ->toArray();
        dd($res['data']);
        return '前台主页，暂无内容';
    }
    public function wheelPicture(Request $request){//首页轮播
        $model = WheelPicture::query();
        $res = $model
            ->where('start_time','<=',date('Y-m-d H:i:s'))
            ->where('end_time','>',date('Y-m-d H:i:s'))
            ->orderBy('weight','desc')
            ->limit($request->get('limit',10))
            ->get(['title','hit_type','hit_content','thumb'])
            ->toArray();
        $data = ['code' => 0];
        if(!empty($res)){
            $data = [
                'code' => 1,
                'data'  => $res
            ];
        }
        return response()->json($data);
    }
    public function navigation(Request $request){//首页导航
        $model = Navigation::query();
        $res = $model
            ->where('start_time','<=',date('Y-m-d H:i:s'))
            ->where('end_time','>',date('Y-m-d H:i:s'))
            ->orderBy('weight','desc')
            ->limit($request->get('limit',10))
            ->get(['title','hit_type','hit_content','thumb'])
            ->toArray();
        $data = ['code' => 0];
        if(!empty($res)){
            $data = [
                'code' => 1,
                'data'  => $res
            ];
        }
        return response()->json($data);
    }
    public function rage(Request $request){//首页爆款
        $model = Rage::query();
        $res = $model
            ->where('start_time','<=',date('Y-m-d H:i:s'))
            ->where('end_time','>',date('Y-m-d H:i:s'))
            ->orderBy('weight','desc')
            ->limit($request->get('limit',10))
            ->get(['title','hit_type','hit_content','thumb'])
            ->toArray();
        $data = ['code' => 0];
        if(!empty($res)){
            $data = [
                'code' => 1,
                'data'  => $res
            ];
        }
        return response()->json($data);
    }
    public function operatePosition(Request $request){
        $model = OperatePosition::query();
        $res = $model
            ->where('start_time','<=',date('Y-m-d H:i:s'))
            ->where('end_time','>',date('Y-m-d H:i:s'))
            ->orderBy('weight','desc')
            ->limit($request->get('limit',10))
            ->get(['internal_name','title','abstract','hit_type','hit_content','thumb'])
            ->toArray();
        $data = ['code' => 0];
        if(!empty($res)){
            $data = [
                'code' => 1,
                'data'  => $res
            ];
        }
        return response()->json($data);
    }
    public function hotWords(Request $request){
        $model = HotWords::query();
        $res = $model
            ->where('start_time','<=',date('Y-m-d H:i:s'))
            ->where('end_time','>',date('Y-m-d H:i:s'))
            ->orderBy('weight','desc')
            ->limit($request->get('limit',10))
            ->get(['show_hot_word','search_word','font_color'])
            ->toArray();
        $data = ['code' => 0];
        if(!empty($res)){
            $data = [
                'code' => 1,
                'data'  => $res
            ];
        }
        return response()->json($data);
    }
    public function recommend(Request $request){
        $model = Recommend::query();
        $res = $model
            ->where('start_time','<=',date('Y-m-d H:i:s'))
            ->where('end_time','>',date('Y-m-d H:i:s'))
            ->orderBy('weight','desc')
            ->limit($request->get('limit',10))
            ->get(['recommend_word','search_word'])
            ->toArray();
        $data = ['code' => 0];
        if(!empty($res)){
            $data = [
                'code' => 1,
                'data'  => $res
            ];
        }
        return response()->json($data);
    }
    public function oponScreen(Request $request){
        $model = OpenScreen::query();
        $res = $model
            ->where('start_time','<=',date('Y-m-d H:i:s'))
            ->where('end_time','>',date('Y-m-d H:i:s'))
            ->orderBy('weight','desc')
            ->limit($request->get('limit',10))
            ->get(['title','hit_type','hit_content','thumb','second'])
            ->toArray();
        $data = ['code' => 0];
        if(!empty($res)){
            $data = [
                'code' => 1,
                'data'  => $res
            ];
        }
        return response()->json($data);
    }
}
